﻿using LayuiMvc.DataEntity;
using LayuiMvc.DataModel.Community;
using LayuiMvcUtil.Result;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LayuiMvc.Logic.Community
{

    /// <summary>
    /// 用户业务逻辑类
    /// </summary>
    public class UserService
    {
        #region 用户个人主页信息
        /// <summary>
        ///用户个人主页信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<DataResult<UserCenterModel>> UserCenterAsync(int userId)
        {
            DataResult<UserCenterModel> rtnResult = new DataResult<UserCenterModel>();

            using(EFContext db = new EFContext())
            {
                UserCenterModel userCenterModel = await (from userEntity in db.Users
                                                         where userEntity.Id == userId
                                                         let publishList = (from postEntity in db.Posts
                                                                            where postEntity.PublishUserId == userId
                                                                            select new PostPublishModel()
                                                                            {
                                                                                PostId = postEntity.PostId,
                                                                                Title = postEntity.Title,
                                                                                PublishTime = postEntity.PublishTime,
                                                                                ReplyCount = postEntity.Replys.Count,
                                                                                ViewCount = postEntity.ViewCount,
                                                                                IsTop = postEntity.IsTop,
                                                                                IsCream = postEntity.IsCream
                                                                            }).Take(30).ToList()
                                                         let replyList = (from replyEntity in db.PostReplys
                                                                          where replyEntity.ReplyUserId == userId
                                                                          select new PostReplyModel()
                                                                          {
                                                                              ReplyTime = replyEntity.ReplyTime,
                                                                              HtmlContent = replyEntity.ReplyContentHtml,
                                                                              PostId = replyEntity.PostId,
                                                                              Title = replyEntity.Post.Title

                                                                          }).Take(30).ToList()
                                                         select new UserCenterModel()
                                                         {
                                                             UserId = userEntity.Id,
                                                             NickName = userEntity.NickName,
                                                             Sex = userEntity.Sex,
                                                             UserIdentity = userEntity.UserIdentity,
                                                             IsApprove = userEntity.IsApprove,
                                                             ApproveMsg = userEntity.ApproveMsg,
                                                             RegisterTime = userEntity.RegisterTime,
                                                             Instrduce = userEntity.Introduce,
                                                             HeadImgUrl = userEntity.HeadImgUrl,
                                                             PublishList = publishList,
                                                             ReplyList = replyList
                                                         }).FirstOrDefaultAsync();
                rtnResult.data = userCenterModel;
            }
            return rtnResult;
        }
        #endregion

        #region 用户发布帖子列表
        /// <summary>
        /// 用户发布的帖子列表
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="pageIndex">当前页的索引</param>
        /// <param name="pageSize">每页的记录条数</param>
        /// <returns></returns>
        public async Task<DataResult<List<UserPublishModel>>> UserPublishListAsync(int userId,int pageIndex,int pageSize)
        {
            DataResult<List<UserPublishModel>> rtnResult = new DataResult<List<UserPublishModel>>();

            using(EFContext db=new EFContext())
            {
                var query = db.Posts.Where(m => !m.IsDelete && m.PublishUserId == userId);
                rtnResult.count = await query.CountAsync();

                List<UserPublishModel> publishList = await query.OrderByDescending(m => m.PublishTime).Skip((pageIndex - 1) * pageSize).Take(pageSize)
                    .Select(m => new UserPublishModel()
                    {
                        PostId = m.PostId,
                        Title = m.Title,
                        PublishTime = m.PublishTime,
                        ViewCount = m.ViewCount,
                        ReplyCount = m.Replys.Count,
                        IsTop = m.IsTop,
                        IsCream = m.IsCream
                    }).ToListAsync();

                rtnResult.data = publishList;
            }
            return rtnResult;
        }
        #endregion

        #region 用户收藏的帖子列表

        public async Task<DataResult<List<UserCollectionModel>>> UserCollectionListAsync(
            int userId,int pageIndex,int pageSize)
        {
            DataResult<List<UserCollectionModel>> rtnResult = new DataResult<List<UserCollectionModel>>();

            using(EFContext db=new EFContext())
            {
                var query = db.PostCollections.Where(m => m.CollectionUserId == userId);
                rtnResult.count = await query.CountAsync();

                List<UserCollectionModel> collectionList = await query.OrderByDescending(m => m.CollectionTime).Skip((pageIndex - 1) * pageSize).Take(pageSize)
                    .Select(m => new UserCollectionModel()
                    {
                        PostId = m.PostId,
                        Title = m.Post.Title,
                        CollectionTime = m.CollectionTime
                    }).ToListAsync();

                rtnResult.data = collectionList;
            }

            return rtnResult;
        }
        #endregion

    }
}
